<?php


namespace App\Helpers;

use Illuminate\Support\Facades\DB;

function successMsg($data = null)
{
    return json_encode(['code' => 0,'msg' => 'SUCCESS','data' => $data],JSON_UNESCAPED_UNICODE);//,JSON_UNESCAPED_UNICODE
}
function errorMsg($code,$msg)
{
    return json_encode(['code' => $code,'msg' => $msg,'data' => null],JSON_UNESCAPED_UNICODE);//,JSON_UNESCAPED_UNICODE
}

function createdGuid($namespace = '')
{
    $guid = time();
    $uid = uniqid("", true);
    $data = $namespace;
    $data .= time();
    $data .= rand(1000,9999);
    $data .= rand(10000,99999);
    $data .= date("Y-m-d H:i:s",time());
    $hash = strtoupper(hash('ripemd128', $uid . $guid . md5($data)));
    $guid =
        substr($hash, 0, 8) .
        '-' .
        substr($hash, 8, 4) .
        '-' .
        substr($hash, 12, 4) .
        '-' .
        substr($hash, 16, 4) .
        '-' .
        substr($hash, 20, 12) ;
    return $guid;
}
function createTableChapter($year)
{
    $sql = "CREATE TABLE `chapter_" . $year . "` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `book_id` int(10) DEFAULT NULL COMMENT '书籍ID',
  `book_uni_code` varchar(255) DEFAULT NULL COMMENT '书籍唯一码',
  `uni_code` varchar(100) DEFAULT NULL," . "  `name` varchar(200) DEFAULT NULL COMMENT '章节名称',
  `index` int(10) DEFAULT NULL COMMENT '章节索引',
  `word` int(11) DEFAULT NULL COMMENT '章节字数',
  `is_vip` tinyint(2) DEFAULT NULL COMMENT '0否 1vip 是否是VIP章节'," . "  `free` int(10) DEFAULT NULL COMMENT '收费钱数 0免费章节',
  `year` varchar(10) DEFAULT NULL,
  `month` varchar(10) DEFAULT NULL,
  `created_at` datetime DEFAULT NULL,
  `update_at` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP," . "  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4;";
    return DB::unprepared($sql);
}
function createTableContent($year)
{
    $sql = "CREATE TABLE `content_$year` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `chapter_id` int(11) DEFAULT NULL COMMENT '章节ID',
  `content` text COMMENT '内容',
  `created_at` datetime DEFAULT NULL,
  `updated_at` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;";
    return DB::unprepared($sql);
}

//获取到上周的 起始 日期
function getLastStartWeekDate()
{
    $date = date("Y-m-d");  //当前日期
    $first=1;   //表示每周星期一为开始时间 0表示每周日为开始时间
    $w = date("w", strtotime($date));   //获取当前周的第几天 周日是 0 周一 到周六是 1 -6
    $d = $w ? $w - $first : 6;  // //如果是周日 -6天
    $now_start = date("Y-m-d", strtotime("$date -".$d." days"));//本周开始时间
    return date('Y-m-d',strtotime("$now_start - 7 days")); //上周开始时间
}
//获取到上周 结束 时间
function getLastEndWeekDate()
{
    $date = date("Y-m-d");  //当前日期
    $first=1;   //表示每周星期一为开始时间 0表示每周日为开始时间
    $w = date("w", strtotime($date));   //获取当前周的第几天 周日是 0 周一 到周六是 1 -6
    $d = $w ? $w - $first : 6;  // //如果是周日 -6天
    $now_start = date("Y-m-d", strtotime("$date -".$d." days")); //本周开始时间
    return date('Y-m-d',strtotime("$now_start- 1 days")); //上周结束时间
}

// des_ecb 对称加密
function des_ecb_encrypt($data){
    $data = json_encode($data);
    $key = 'sdfkjhkjsjgkdhgkykjdsafkwqegwervsdklsdj';
    $iv = substr($key, 0, 16);
    return openssl_encrypt($data, 'des-ecb', $key);
}

// des_ecb 对称解密
function des_ecb_decrypt(){
    $data = "Hni6RwlMCoHwVpRfpndZZr+jOa3UH3sBS1ByFLaIdHxfRVoORERLkRKVYAd62douwsMeZ9mM3cgCoBv7VdvF/YNShoA/a7ShJeQDnSDHqj6xqEgokqEaCeiuchEoFJdFBnk8yYdMVkx/9ZyX1Hecc/fA+/7XBNkIxXKgAmEZvPSLQ6uTF9yhMyuu8rnTlJtbYmUH1p2p4ryQvMAnfWU9Ve01/fjQMjFpGM1mU5viZiiVZSHw9KgGeaMJyIU3CMCI4vHckbmmFS8gqxfu+I2eTieSqz3Arn7JGqFIGssQbHmnkcDlR/s1pJRNpBwVdW23Fg/qgpNURy9Za87XfhP9J/ujp/hT5tuhCea3U2PoYOVJgRWrAWzOzA==";
    $key = 'sdfkjhkjsjgkdhgkykjdsafkwqegwervsdklsdj';
    $iv = substr($key, 0, 16);
    $data = openssl_decrypt($data, 'des-ecb', $key);
    var_dump(json_decode($data));
}
// 加密解密
function des_cbc_decrypt($data){
    $plaintext = json_encode($data);
    $cipher = "aes-128-gcm";
    $key = 'sdfkjhkjsjgkdhgkykjdsafkwqegwervsdklsdj';
    if (in_array($cipher, openssl_get_cipher_methods()))
    {
        $ivlen = openssl_cipher_iv_length($cipher);
        $iv = openssl_random_pseudo_bytes($ivlen);
        $ciphertext = openssl_encrypt($plaintext, $cipher, $key, $options=0, $iv, $tag);
        $original_plaintext = openssl_decrypt($ciphertext, $cipher, $key, $options=0, $iv, $tag);
        echo $original_plaintext."\n";
    }
}

